package com.ds.lens.data.common.query.es;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.ElasticSearchDruidDataSourceFactory;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.Properties;

import static com.alibaba.druid.pool.DruidDataSourceFactory.PROP_CONNECTIONPROPERTIES;
import static com.alibaba.druid.pool.DruidDataSourceFactory.PROP_URL;

public class ESJDBCTest {
    public static void main(String[] args) throws Exception {
        Properties properties = new Properties();
        properties.put(PROP_URL, "jdbc:elasticsearch://172.20.45.1:9300");
        properties.put(PROP_CONNECTIONPROPERTIES, "client.transport.ignore_cluster_name=true");
        DruidDataSource dds = (DruidDataSource) ElasticSearchDruidDataSourceFactory.createDataSource(properties);
        Connection connection = dds.getConnection();
        String sql = "SELECT transName, avg(appRequest.totalRespTime) as avgResponseTime FROM transaction-aggr-1 where  timeStamp between 1571154900000 and 1571155260000 GROUP BY terms('field'='transName', 'size'='5', 'alias'='transName')";
        String sql1 = "select uid from event limit 10";
        PreparedStatement ps = connection.prepareStatement(sql1);
        ResultSet resultSet = ps.executeQuery();

        ResultSetMetaData metaData = resultSet.getMetaData();
        for (int i = 1; i <= metaData.getColumnCount(); i ++) {
            System.out.println(metaData.getColumnName(i));
        }

        while (resultSet.next()) {
//            String scrollId = resultSet.getString("_scroll_id");
            String uid = resultSet.getString("uid");
            System.out.println(uid);
        }

        ps.close();
        connection.close();
        dds.close();
    }
}
